package org.easyway.tiles.generic;

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.Serializable;

public class CustomImage implements Serializable {
	private static final long serialVersionUID = 5883197445030442981L;

	public transient Image img;

	public String filename;

	public String type = "$_TILE";

	/** to implement. */
	public boolean maskered = false;

	public transient static GraphicBoard board;

	public CustomImage() {
		img = null;
		filename = null;
	}

	public CustomImage(String filename) {
		this.filename = filename;
		MediaTracker mt = GraphicBoard.mt;
		img = Toolkit.getDefaultToolkit().getImage(filename);
		mt.addImage(img, 0);
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
			System.out.println("image loaded: " + filename);
		}

	}

	public CustomImage(CustomImage cimg) {
		img = cimg.img;
		filename = cimg.filename;
	}

	public boolean equals(Object obj) {
		if (obj instanceof CustomImage) {
			System.out.println(filename);
			// filename = filename.replace('/', '\\');
			// ((CustomImage) obj).filename =((CustomImage)
			// obj).filename.replace('/', '\\');
			String sub1, sub2;
			try {
				sub1 = filename.substring(filename.lastIndexOf('\\'));
			} catch (StringIndexOutOfBoundsException e) {
				sub1 = filename.substring(filename.lastIndexOf('/'));
			}
			try {
				sub2 = ((CustomImage) obj).filename
						.substring(((CustomImage) obj).filename
								.lastIndexOf('\\'));
			} catch (StringIndexOutOfBoundsException e) {
				sub2 = ((CustomImage) obj).filename
						.substring(((CustomImage) obj).filename
								.lastIndexOf('/'));
			}
			return sub1.equals(sub2) && type.equals(((CustomImage) obj).type);
		}
		return obj.equals(this);
	}
}
